import { Controller } from "../../mvc/core/Controller";
import { IPlayerRootController } from "./interfaces/IPlayerRootController";
import { IView } from "../../mvc/interfaces/IView";
import { GameController } from "../mvc/GameController";
import { IPlayerModule } from "../../../modules/interfaces/IPlayerModule";
import { IPlayerRootSavvyModule } from "../../../modules/interfaces/IPlayerRootSavvyModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { AcupointData } from "../view/player/acupoint/AcupointData";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAcupointInfo } from "../../../Message/Game/SAcupointInfo";
type int = number;
//class PlayerRootController
    
    export  class PlayerRootController extends Controller implements IPlayerRootController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._rootModule == null) 
            {
                this._rootModule = (GameController.player.view as IPlayerModule).rootSavvyModule;
            }
            return this._rootModule;
        }

        public get rootModule(): IPlayerRootSavvyModule
        {
            this.initView();
            return this._rootModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.AcupointOpenRootModule, this.onRootModuleOpenHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeRoot, this.onUpgradeRootHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeRootSuccess, this.onUpgradeRootSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointRootBreakThrough, this.onRootBreakThroughHandler);
            Dispatcher.addEventListener(EventName.AcupointRootBreakThroughSuccess, this.onRootBreakThroughSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointRootNeedBreak, this.onRootNeedBreakHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBagDataChangehandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAcupointInfo, this.onAcupointInfoReciveHandler);
            return;
        }

        /* internal  */onRootModuleOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.rootModule.show(430, -5);
            this.rootModule.showPanel(0);
            this.rootModule.setCurrentRoot(loc1);
            return;
        }

        /* internal  */onUpgradeRootHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.step as int;
            var loc3=loc1.useProtectNum as int;
            var loc4=loc1.perfectUpgrade as Boolean;
            GameProxy.acupoint.upgradeRoot(loc2, loc3, loc4);
            return;
        }

        /* internal  */onUpgradeRootSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.step;
            var loc3=loc1.newLevel;
            var loc4=loc1.result;
            var loc5=loc1.effect;
            var loc6=loc1.acupointAttribute;
            AcupointData.setRootLevel(loc2, loc3);
            AcupointData.setRootEffects(loc2, loc5);
            AcupointData.setAttributesByStep(loc2, loc6);
            this.rootModule.setConfigInfo(loc2);
            this.rootModule.setRootInfo(loc2);
            if (loc4) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30201));
                SoundManager.instance.soundPlay(SoundTypeConst.GenGuS);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(30202));
                SoundManager.instance.soundPlay(SoundTypeConst.GenGuF);
            }
            return;
        }

        /* internal  */onRootBreakThroughHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Object;
            var loc2=loc1.step as int;
            var loc3=loc1.props as Map<any, any> /* flash.utils.Dictionary */;
            GameProxy.acupoint.rootBreakThrough(loc2, loc3);
            return;
        }

        /* internal  */onRootBreakThroughSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["result"] as Boolean;
            var loc2=arg1.data["step"] as int;
            if (loc1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30211));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(30212));
            }
            if (this._rootModule && !this._rootModule.isHide) 
            {
                this.rootModule.updateRootBreakThrough(loc2);
            }
            return;
        }

        /* internal  */onRootNeedBreakHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (!(this._rootModule == null) && !this._rootModule.isHide) 
            {
                this.rootModule.goToRootBreakThrough(loc1);
            }
            return;
        }

        /* internal  */onBagDataChangehandler(arg1: Object): void
        {
            this.initView();
            if (!(this._rootModule == null) && !this._rootModule.isHide) 
            {
                this.rootModule.updateProtectProp();
            }
            return;
        }

        /* internal  */onAcupointInfoReciveHandler(arg1: MessageBlock): void
        {
            var loc2=0;
            var loc1=arg1.messageBase as SAcupointInfo;
            AcupointData.setInfo(loc1);
            if (!(this._rootModule == null) && !this._rootModule.isHide) 
            {
                loc2 = loc1.step;
                if (loc2 > 1 && loc2 <= 8) 
                {
                    --loc2;
                }
                else if (loc2 == 1) 
                {
                    loc2 = 8;
                }
                this.rootModule.updateRootConfigInfo(loc2);
                this.rootModule.updateRootInfo(loc2);
            }
            return;
        }

        private /* var */_rootModule: IPlayerRootSavvyModule;
    }
